package org.real.guetshop.Task;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.real.guetshop.constant.OrderConstant;
import org.real.guetshop.mapper.OrderMapper;
import org.real.guetshop.pojo.entity.Order;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class OrderTask {

    @Resource
    private OrderMapper orderMapper;

    /**
     * 处理已完成的订单
     */
    @Scheduled(cron = "0 0 1 * * ?")    // 每天凌晨1点执行
    public void processFinishOrder(){
        log.info("自动处理两天前已完成订单：{}",new Date());

        // 获取两天前的时间
        LocalDateTime nowTime = LocalDateTime.now().minusDays(2);

        List<Order> orderList = orderMapper.getByStatus(OrderConstant.IS_FINISH);
        if(orderList != null && orderList.size() > 0){
            orderList.forEach(order -> {
                orderMapper.deleteByOverTime(order,nowTime);
            });
        }
    }
}
